package com.stu.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.stu.common.CreateUUID;
import com.stu.dao.StuDao;
import com.stu.dao.impl.StuDaoImpl;
import com.stu.model.Stu;
import com.stu.service.StuService;

public class StuServiceImpl implements StuService{
	private static StuDao dao=StuDaoImpl.getStuDaoImpl();

	private static StuServiceImpl service=new StuServiceImpl();
	private StuServiceImpl() {}
	public static StuServiceImpl getStuServiceImpl() {
		return service;
	}
	
	@Override
	public boolean save(Stu stu) {
		String sql="insert into stu(id,loginname,password,sex) values('"+CreateUUID.getUUID()+"','"+stu.getLoginname()+"','"+stu.getPassword()+"','"+stu.getSex()+"') ";
		boolean b=false;
		try {
			b=dao.saveOrUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	@Override
	public List<Stu> findAll() {
		String sql="select id,loginname,password,sex from stu";
		try {
			List<Stu> list=dao.findAll(sql);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	@Override
	public Stu findById(String id) {
		String sql="select id,loginname,password,sex from stu where id='"+id+"'";
		try {
			Stu stu=dao.findAll(sql).get(0);
			return stu;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	@Override
	public boolean update(Stu stu) {
		String sql="update stu set loginname='"+stu.getLoginname()+"',password='"+stu.getPassword()+"',sex='"+stu.getSex()+"' where id='"+stu.getId()+"'";
		try {
			return dao.saveOrUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean delete(String id) {
		String sql="delete from stu where id='"+id+"'";
		try {
			return dao.saveOrUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
